package com.edge.pbx.view.summing;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.DrawableRes;
import androidx.annotation.RequiresApi;

import com.edge.pbxdsp.R;
import com.qztech.utils.YzDebug;


/**
 * Copyright (C), 深圳市东乘电子科技有限公司
 * Carate By YangZhong
 */
public class PbxOutputSelectChannelsLayout extends ViewGroup {
    public static final int sColorNormal = 0xFF696b72;
    public static  int sColorSelected = 0xFFee6b1b;

    private Drawable mDrawableLine, mDrawableSpeakerL,mDrawableSpeakerR;
//    private Drawable mDrawableLineS, mDrawableSpeakerLS,mDrawableSpeakerRS;
//    private int mSelectChannel = 0;
    final int mBaseCarW = 448;
    final int mBaseCarH = 928;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public PbxOutputSelectChannelsLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mDrawableSpeakerL =  context.getResources().getDrawable(R.drawable.pbx_summing_speaker_l_n);
//        mDrawableSpeakerLS = context.getResources().getDrawable(R.drawable.pbx_summing_speaker_l_s);
        mDrawableSpeakerR =  context.getResources().getDrawable(R.drawable.pbx_summing_speaker_r_n);
//        mDrawableSpeakerRS = context.getResources().getDrawable(R.drawable.pbx_summing_speaker_r_s);
        mDrawableLine = context.getResources().getDrawable(R.drawable.pbx_summing_speaker_line_n);
//        mDrawableLineS = context.getResources().getDrawable(R.drawable.pbx_summing_speaker_line_n);

    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        for (int i = 0, len = getChildCount(); i < len ; i++ ){
            getChildAt(i).setEnabled(enabled);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int widht = MeasureSpec.getSize(widthMeasureSpec);
        final int height = MeasureSpec.getSize(heightMeasureSpec);


        View view = null;
        view = getChildAt(0);
        view.measure(MeasureSpec.makeMeasureSpec(mBaseCarW *height/ mBaseCarH,MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));

        final int baseChannelW = 302;
        final int baseChannelH = 202;
        int channelH = baseChannelH*height/ mBaseCarH;
        int channelW = channelH*baseChannelW/baseChannelH;
        for (int i = 1, count = getChildCount(); i < count ; i++){
            view = getChildAt(i);
            view.measure(MeasureSpec.makeMeasureSpec(channelW,MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(channelH,MeasureSpec.EXACTLY));
        }
    }



    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int width = r -l;
        final int height = b - t;
        View view = null;
        view = getChildAt(0);
        l = (width - view.getMeasuredWidth())/2;
        t = (height - view.getMeasuredHeight())/2;
        view.layout(l, t , l + view.getMeasuredWidth(), t +view.getMeasuredHeight());
        view = getChildAt(1);
        int channelW = view.getMeasuredWidth();
        int channelH = view.getMeasuredHeight();
        int offset = (int) (width*0.01f);
        int vPadding = (height - channelH*4)/5;
        for (int i = 0, count = 4; i < count; i++){
            view = getChildAt(i+1);
            t =  i*(vPadding+channelH) +vPadding;
            view.layout(offset, t, offset+channelW, t+channelH);
        }

        r = width - offset;
        for (int i = 0, count = 4; i < count; i++){
            view = getChildAt(i+5);
            t =  i*(vPadding+channelH) +vPadding;
            view.layout(r - channelW, t, r, t+channelH);
        }
    }


    public void setCarTypeDrawable(@DrawableRes int id){
        final View view = getChildAt(0);

        if (view != null && id != View.NO_ID){
            view.setBackgroundResource(id);
        }
    }
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        View view = null;
        View carView = getChildAt(0);
        int carLeft = (getWidth()-carView.getMeasuredWidth())/2;
        int carRight = carLeft + carView.getMeasuredWidth();

        int speakerSize =  85 * carView.getMeasuredHeight()/mBaseCarH;
        View channelView = getChildAt(1);
        int channelW =  channelView.getMeasuredWidth();
        int chennelH = channelView.getMeasuredHeight();
        int lineXOffset = (int) (speakerSize * 0.1f);
        int speaketYOffset = (chennelH - speakerSize)/2;
        int lineH = 2;
        int lineYOffset = (chennelH - lineH)/2;
        int top = 0;
        int left = 0;
        int index;
        Drawable drawableSpeaker, drawableLine;
        for (int i = 0, len = 4; i < len; i++){
            index = i+1;
            view = getChildAt(index);
//            YzDebug.d("Yang", i +" >>>>>>>>>>>>>>:" + view.isSelected()+" count:"+ getChildCount());

//            if (view.isSelected())
//            {
//                drawableSpeaker = mDrawableSpeakerLS;
//                drawableLine = mDrawableLineS;
//            }else
                {
                drawableSpeaker = mDrawableSpeakerL;
                drawableLine = mDrawableLine;
            }
            if(view.isSelected()){
                drawableSpeaker.setColorFilter(sColorSelected, PorterDuff.Mode.MULTIPLY);
                drawableLine.setColorFilter(sColorSelected, PorterDuff.Mode.MULTIPLY);
            }else {
                drawableSpeaker.setColorFilter(sColorNormal, PorterDuff.Mode.MULTIPLY);
                drawableLine.setColorFilter(sColorNormal, PorterDuff.Mode.MULTIPLY);
            }


            top = view.getTop()+speaketYOffset;
            drawableSpeaker.setBounds(carLeft-speakerSize,top,carLeft,top +speakerSize);
            drawableSpeaker.draw(canvas);

            left = view.getRight()+lineXOffset*2;
            top = view.getTop() + lineYOffset;
            drawableLine.setBounds(left,top,carLeft-speakerSize -lineXOffset,top+lineH);
            drawableLine.draw(canvas);
        }

        for (int i = 0, len = 4; i < len; i++){
            index = i+5;
            view = getChildAt(index);
//            if (view.isSelected()){
//                drawableSpeaker = mDrawableSpeakerRS;
//                drawableLine = mDrawableLineS;
//            }else
                {
                drawableSpeaker = mDrawableSpeakerR;
                drawableLine = mDrawableLine;
            }
            if(view.isSelected()){
                drawableSpeaker.setColorFilter(sColorSelected, PorterDuff.Mode.MULTIPLY);
                drawableLine.setColorFilter(sColorSelected, PorterDuff.Mode.MULTIPLY);
            }else {
                drawableSpeaker.setColorFilter(sColorNormal, PorterDuff.Mode.MULTIPLY);
                drawableLine.setColorFilter(sColorNormal, PorterDuff.Mode.MULTIPLY);
            }
            top = view.getTop()+speaketYOffset;

            drawableSpeaker.setBounds(carRight,top,carRight+speakerSize,top +speakerSize);
            drawableSpeaker.draw(canvas);

            left = carRight + speakerSize + lineXOffset;
            top = view.getTop() + lineYOffset;
            drawableLine.setBounds(left,top,view.getLeft()-lineXOffset*2,top+lineH);
            drawableLine.draw(canvas);
        }
    }
}
